const app = getApp()
const {
    toUrl,
    globalData
} = getApp();
import {
    request
} from '@/utils/request'
Page({
    toUrl: app.toUrl,
    alert() {
        this.setData({
            alert: !this.data.alert
        })
    },
    sort({
        currentTarget
    }) {
        const {
            sort
        } = currentTarget.dataset
        this.setData({
            alert: false,
            sort,
            page: 1,
            count: 0,
            list: []
        })
        this.getList()
    },
    data: {
        copyIt: {},
        globalData,
        alert: false,
        list: [],
        count: 0,
        sort: 0, // 收藏量 1-小到大 2-大到小
        page: 1,
        keywords: '',
        field_type: 2, // field_type 1-楼层，2-类别
        field_value: 0,
        catelist: [], //类别
        storeylist: [], //楼层
        inputvalue: "",
        isScrollToLower: false,
        scrollTop: 0
    },

    field_value({
        currentTarget
    }) {
        const {
            value
        } = currentTarget.dataset
        this.setData({
            field_value: value,
            isScrollToLower: false,
            scrollTop: 0
        })
        this.getList(1)
    },
    field_type({
        currentTarget
    }) {
        const {
            type
        } = currentTarget.dataset
        this.setData({
            field_value: 0,
            field_type: type,
            isScrollToLower: false,
            scrollTop: 0
        })
        this.getList(1)
    },
    clearKeywords() {
        if (this.data.keywords) {
            this.setData({
                page: 1,
                keywords: '',
                inputvalue: ""
            });
            this.getList(1)
        }
    },
    keywords(e) {
        this.setData({
            page: 1,
            keywords: e.detail.value
        })
        this.getList(1)
    },
    getList(page = 1) {
        const that = this
        let {
            keywords,
            sort,
            field_type,
            field_value,
            list
        } = this.data
        if (page == 1) {
            list = []
        }
        return request('stores/storelist', {
            data: {
                page,
                keywords,
                sort,
                field_type,
                field_value
            }
        }).then(({
            data,
            count
        }) => {
            list = list.concat(data)

            // if (count == this.data.page || this.data.page == 1) {
            //     let [first,...rest] = list
            //     list = [...rest,first]
            // }

            that.setData({
                list: list,
                page: page + 1,
                count: count
            })

        })

    },

    onScrollToLower: function () {
        if (this.data.isScrollToLower == true) {
            return
        }
        this.setData({
            isScrollToLower: true
        });
        if (this.data.page <= this.data.count) {
            this.getList(this.data.page)
            this.setData({
                isScrollToLower: false
            });
        }
    },
    onLoad(options) {
        wx.showLoading({
            title: '加载中'
        })
        if (options.field_type != undefined && options.field_value != undefined) {
            this.setData({
                field_value: options.field_value,
                field_type: options.field_type,
            })
        }

        const {
            storeylist
        } = app.globalData
        if (storeylist && storeylist.length > 0) {
            this.setData({
                storeylist
            })
        } else {
            request('currency/storeylist').then(({
                data
            }) => {
                app.globalData.storeylist = data
                this.setData({
                    storeylist: data
                })
            })
        }
        request('currency/catelist', {
            data: {
                type: 2
            }
        }).then(({
            data
        }) => {
            this.setData({
                catelist: data
            })
        })
        if (options.keywords != undefined) {
            this.setData({
                page: 1,
                keywords: options.keywords,
                inputvalue:options.keywords
            })
        }
        this.getList(1).then(() => {
            setTimeout(() => {
                wx.hideLoading()
            }, 500);
        })


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        //console.log("上拉加载...");
        if (this.data.page <= this.count) {
            this.getList(this.data.page)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})